package com.petshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.petshop.pojo.Follow;
import com.petshop.service.FollowService;
import com.petshop.mapper.FollowMapper;
import com.petshop.utils.Result;
import com.petshop.utils.UserHolder;
import org.springframework.stereotype.Service;

/**
* @author 20289
* @description 针对表【tb_follow】的数据库操作Service实现
* @createDate 2025-04-04 22:58:19
*/
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow>
    implements FollowService{

    /**
     * 关注和取消关注的方法
     *
     * @param followUserId
     * @param isFollow
     * @return
     */
    @Override
    public Result follow(Long followUserId, Boolean isFollow) {
        //获取当前登录用户
        Integer userId = UserHolder.getUser().getUserId();
        //1.判断应该是关注还是取关
        if (isFollow) {
            //2.关注，新增一条关注用户与被关注用户的关系的数据
            Follow follow = new Follow();
            follow.setUserId(Long.valueOf(userId));
            follow.setFollowUserId(followUserId);
            save(follow);
        }else {
            //3.取关就删除一条关注用户与被关注用户的关系的数据,
            // delete from tb_follow where user_id =? and follow_user_id = ?
            LambdaQueryWrapper<Follow> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Follow::getUserId,userId);
            queryWrapper.eq(Follow::getFollowUserId, followUserId);
            remove(queryWrapper);
        }
        return Result.ok(null);
    }

    /**
     * 判断是否关注
     *
     * @param followUserId
     * @return
     */
    @Override
    public Result isFollow(Long followUserId) {
        //1.获取当前登录用户
        Integer userId = UserHolder.getUser().getUserId();
        //2.查询是否关注
        //select* from tb_follow where user_id =? and follow_user_id = ?
        Long count = lambdaQuery()
                .eq(Follow::getFollowUserId, followUserId)
                .eq(Follow::getUserId, userId).count();
        //3.判断是否关注
        return Result.ok(count>0);
    }
}




